package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Rule;
import cn.edu.scau.cmi.ema.domain.Rulefield;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("RulefieldController")
public class RulefieldController extends CmiEmaController {

	@RequestMapping("/listRulefields")
	public ModelAndView listRulefields(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = rulefieldPaginationService.updatePagination(request, pagination);
		Set<Rulefield> rulefields = rulefieldPaginationService.getPaginationRulefields(pagination);

		mav.addObject("rulefields", rulefields);
		mav.addObject("entityName", "rulefield");
		mav.addObject("pagination", pagination);
		mav.setViewName("rulefield/listRulefields");
		return mav;
	}

	public String listRulefields() {
		return "redirect:/listRulefields";
	}

	@RequestMapping("/viewRulefield")
	public ModelAndView viewRulefield(@RequestParam Integer rulefieldId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("rulefield", rulefieldDAO.findRulefieldByPrimaryKey(rulefieldId));
		mav.setViewName("rulefield/viewRulefield");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteRulefields")
	public ModelAndView confirmDeleteRulefields(
			@RequestParam(value = "deleteRulefieldsIds", required = true) Set<Integer> deleteRulefieldsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Rulefield> deleteRulefields = rulefieldDAO.findRulefieldsByIds(deleteRulefieldsIds);
		Set<Rulefield> referencedRulefields = rulefieldService.loadReferenceRulefields(deleteRulefields);
		boolean hasReferenced;
		if (referencedRulefields.size() == 0) {
			mav.addObject("deleteEntitys", deleteRulefields);
			hasReferenced = false;
		} else {
			deleteRulefields.removeAll(referencedRulefields);
			mav.addObject("referencedEntitys", referencedRulefields);
			mav.addObject("deleteEntitys", deleteRulefields);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Rulefield");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listRulefields");
		return mav;
	}

	@RequestMapping("/deleteRulefield")
	public String deleteRulefield(@RequestParam Integer rulefieldId) {
		Rulefield rulefield = rulefieldDAO.findRulefieldByPrimaryKey(rulefieldId);
		rulefieldService.deleteRulefield(rulefield);
		return "forward:/listRulefields";
	}

	@RequestMapping("/confirmDeleteRulefield")
	public ModelAndView confirmDeleteRulefield(@RequestParam Integer rulefieldId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("rulefield", rulefieldDAO.findRulefieldByPrimaryKey(rulefieldId));

		mav.setViewName("rulefield/deleteRulefield");

		return mav;
	}

	@RequestMapping("/deleteRulefields")
	public String deleteRulefields(
			@RequestParam(value = "deleteRulefieldsId", required = true) Set<Integer> deleteRulefieldsId) {
		rulefieldService.deleteRulefields(deleteRulefieldsId);
		return "forward:/listRulefields";
	}

	@RequestMapping("/editRulefield")
	public ModelAndView editRulefield(@RequestParam Integer rulefieldId) {

		ModelAndView mav = new ModelAndView();

		Rulefield rulefield = rulefieldDAO.findRulefieldByPrimaryKey(rulefieldId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("rulefield", rulefield);

		// 把所有的引用rulefield对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(rulefieldService.loadRulefieldObjectReferencedSet(rulefield));

		// 将编辑页面rulefield添加到mav中
		mav.setViewName("rulefield/editRulefield");
		return mav;
	}

	@RequestMapping("/newRulefield")
	public ModelAndView newRulefield() {
		ModelAndView mav = new ModelAndView();
		Rulefield rulefield = new Rulefield();

		mav.addObject("rulefield", rulefield);

		// 把所有的引用rulefield对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(rulefieldService.loadRulefieldObjectReferencedSet(rulefield));

		mav.addObject("newFlag", true);
		mav.setViewName("rulefield/editRulefield");

		return mav;
	}

	@RequestMapping("/saveRulefield")
	public ModelAndView saveRulefield(@ModelAttribute Rulefield rulefield) {

		ModelAndView mav = new ModelAndView();

		Rule ruleTemp = rulefield.getRule();
		Rule rule = ruleTemp == null ? null : CmiHibernateProxyUtil.deproxy(ruleDAO.findRuleById(ruleTemp.getId()));

		Rulefield existingRulefield = CmiHibernateProxyUtil.deproxy(rulefieldDAO.findRulefieldByPhysicalPrimaryKey(
				rule.getId(), rulefield.getEntityclassname(), rulefield.getEntityfieldname()));

		rulefield.setRule(rule);
		// 新建记录
		if (rulefield.getId() == null) {
			if (rulefield.equals(existingRulefield)) {

				mav.addAllObjects(rulefieldService.loadRulefieldClassReferencedSet(rulefield));
				// mav = editContinue(mav, rulefield);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rulefield/editRulefield");
			} else {
				rulefieldService.saveRulefield(rulefield);
				mav.setViewName("forward:/listRulefields");
			}
		}
		// 编辑记录
		else {
			Rulefield originRulefield = CmiHibernateProxyUtil
					.deproxy(rulefieldDAO.findRulefieldByPrimaryKey(rulefield.getId()));
			if (!rulefield.equals(originRulefield) && rulefield.equals(existingRulefield)) {
				mav.addAllObjects(rulefieldService.loadRulefieldClassReferencedSet(rulefield));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rulefield/editRulefield");
			} else {
				rulefieldService.saveRulefield(rulefield);
				mav.setViewName("forward:/listRulefields");
			}
		}
		return mav;
	}

	@RequestMapping("/rulefieldController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
